from manim import *


class EqualityExplanation(Scene):
    def construct(self):
        # 创建自定义 LaTeX 模板
        tex_template = TexTemplate()
        tex_template.add_to_preamble(r"\usepackage{ctex}")

        # 标题
        title = Text("等号（=） 与 恒等号（≡）", font_size=48, color=YELLOW)
        self.play(Write(title))
        self.wait(1)
        self.play(title.animate.to_edge(UP))

        # 普通等号部分
        equal_sign = Tex(r"普通等号（=）", color=BLUE, tex_template=tex_template).to_edge(2*LEFT)
        self.play(Write(equal_sign))

        # 示例 1: 方程
        equation = MathTex("x + 2 = 5", font_size=36)
        equation.next_to(equal_sign, DOWN, buff=0.3)
        equation_explanation = Tex(r"表示两个表达式在某些条件（即x=3）下相等", font_size=24, tex_template=tex_template)
        equation_explanation.next_to(equation, DOWN, buff=0.2)
        self.play(Write(equation))
        self.play(Write(equation_explanation))
        self.wait(1)

        # 示例 2: 恒等式
        identity = MathTex("(a + b)^2 = a^2 + 2ab + b^2", font_size=36)
        identity.next_to(equation_explanation, DOWN, buff=0.3)
        identity_explanation = Tex(r"表示两个表达式在所有情况下相等（即恒等）", font_size=24, tex_template=tex_template)
        identity_explanation.next_to(identity, DOWN, buff=0.2)
        self.play(Write(identity))
        self.play(Write(identity_explanation))
        self.wait(1)

        # 恒等号部分
        equiv_sign = Tex(r"恒等号（≡）", color=GREEN, tex_template=tex_template).to_edge(2*RIGHT)
        self.play(Write(equiv_sign))

        # 示例 1: 恒等式
        identity_equiv = MathTex("\\sin^2(x) + \\cos^2(x) \\equiv 1", font_size=36)
        identity_equiv.next_to(equiv_sign, DOWN, buff=0.3)
        identity_equiv_explanation = Tex(r"表示两个表达式在所有情况下相等", font_size=24, tex_template=tex_template)
        identity_equiv_explanation.next_to(identity_equiv, DOWN, buff=0.2)
        self.play(Write(identity_equiv))
        self.play(Write(identity_equiv_explanation))
        self.wait(1)

        # 示例 2: 同余关系
        congruence = MathTex("5 \\equiv 2 \\pmod{3}", font_size=36)
        congruence.next_to(identity_equiv_explanation, DOWN, buff=0.2)
        congruence_explanation = Tex(r"表示两个数在模 n 意义下同余", font_size=24, tex_template=tex_template)
        congruence_explanation.next_to(congruence, DOWN, buff=0.2)
        self.play(Write(congruence))
        self.play(Write(congruence_explanation))
        self.wait(1)

        # 示例 3: 定义
        definition = MathTex("f(x) \\equiv \\frac{1}{x}", font_size=36)
        definition.next_to(congruence_explanation, DOWN, buff=0.2)
        definition_explanation = Tex(r"表示一个新的符号或表达式的定义", font_size=24, tex_template=tex_template)
        definition_explanation.next_to(definition, DOWN, buff=0.2)
        self.play(Write(definition))
        self.play(Write(definition_explanation))
        self.wait(1)

        # 保持动画一段时间
        self.wait(2)

class EqualityExplanation2(Scene):
    def construct(self):
        # 创建自定义 LaTeX 模板
        tex_template = TexTemplate()
        tex_template.add_to_preamble(r"\usepackage{ctex}")

        # 标题
        title = Text("等号（=） 与 恒等号（≡）", font_size=48, color=YELLOW)
        self.play(Write(title))
        self.wait(2)
        self.play(title.animate.to_edge(UP))

        # 普通等号部分
        equal_sign = Tex(r"普通等号（=）", color=BLUE, tex_template=tex_template).to_edge(2*LEFT)
        self.play(Write(equal_sign))

        # 示例 1: 方程
        equation = MathTex("x + 2 = 5", font_size=36)
        equation.next_to(equal_sign, DOWN, buff=0.5)
        equation_explanation = Tex(r"表示两个表达式在某些条件（即x=3）下相等", font_size=24, tex_template=tex_template)
        equation_explanation.next_to(equation, DOWN, buff=0.2)
        self.play(Write(equation))
        self.play(Write(equation_explanation))
        self.wait(2)

        # 示例 2: 恒等式
        identity = MathTex("(a + b)^2 = a^2 + 2ab + b^2", font_size=36)
        identity.next_to(equation_explanation, DOWN, buff=0.5)
        identity_explanation = Tex(r"表示两个表达式在所有情况下相等（即恒等）", font_size=24, tex_template=tex_template)
        identity_explanation.next_to(identity, DOWN, buff=0.2)
        self.play(Write(identity))
        self.play(Write(identity_explanation))
        self.wait(2)

        # 恒等号部分
        equiv_sign = Tex(r"恒等号（≡）", color=GREEN, tex_template=tex_template).to_edge(RIGHT)
        self.play(Write(equiv_sign))

        # 示例 1: 恒等式
        identity_equiv = MathTex("\\sin^2(x) + \\cos^2(x) \\equiv 1", font_size=36)
        identity_equiv.next_to(equiv_sign, DOWN, buff=0.5)
        identity_equiv_explanation = Tex(r"表示两个表达式在所有情况下相等", font_size=24, tex_template=tex_template)
        identity_equiv_explanation.next_to(identity_equiv, DOWN, buff=0.2)
        self.play(Write(identity_equiv))
        self.play(Write(identity_equiv_explanation))
        self.wait(2)

        # 示例 2: 同余关系
        congruence = MathTex("5 \\equiv 2 \\pmod{3}", font_size=36)
        congruence.next_to(identity_equiv_explanation, DOWN, buff=0.5)
        congruence_explanation = Tex(r"表示两个数在模 n 意义下同余", font_size=24, tex_template=tex_template)
        congruence_explanation.next_to(congruence, DOWN, buff=0.2)
        self.play(Write(congruence))
        self.play(Write(congruence_explanation))
        self.wait(2)

        # 示例 3: 定义
        definition = MathTex("f(x) \\equiv \\frac{1}{x}", font_size=36)
        definition.next_to(congruence_explanation, DOWN, buff=0.5)
        definition_explanation = Tex(r"表示一个新的符号或表达式的定义", font_size=24, tex_template=tex_template)
        definition_explanation.next_to(definition, DOWN, buff=0.2)
        self.play(Write(definition))
        self.play(Write(definition_explanation))
        self.wait(2)

        # 保持动画一段时间
        self.wait(2)